#ifndef EVENT_MANAGER_H
#define EVENT_MANAGER_H

#include "EventHandler.h"
#include <vector>

using std::vector;

template <typename Event>
class EventManager {
  
  vector<EventHandler<Event>*> handlers_;

 public:
  EventManager() {
  }
  
  // 增加EventHandler
  void AddHandler(EventHandler<Event>* handler) {
    handlers_.push_back(handler);
  }
  
  // 发送事件，即把事件发送给所有的EventHandler处理
  bool SendEvent(Event e) {
    bool result = true;

    for (int i=0; i<handlers_.size(); ++i) {
      if (!handlers_[i]->Handle(e)) {
	result = false;
      }
    }

    return result;
  }

  // 在析构的时候，释放所有的EventHandler内存
  ~EventManager() {
    for (size_t i=0; i<handlers_.size(); ++i) {
      delete handlers_[i];
      //handlers_[i] = NULL;
    }
  }
};

#endif
